/**
 * @Title: MainProjectCreateLogic.java
 * @Package cn.com.gome.generator.logic
 * @Description: 主项目创建
 * @author chenmin-ds
 * @date 2017年5月26日 下午2:49:05
 * @company cn.com.gome
 * @version V1.0
 */


package cn.com.gome.generator.logic;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.Map;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.com.gome.framework.dao.entity.TblChildProjectInfo;
import cn.com.gome.framework.dao.entity.TblProjectBasicInfo;

import com.gomeplus.frame.exception.LogicsException;
import com.gomeplus.frame.logic.ILogics;
import com.gomeplus.frame.logic.ResultEnum;

/**
 * @author chenmin-ds
 * @ClassName: MainProjectCreateLogic
 * @Description: 主项目创建
 * @date 2017年5月26日 下午2:49:05
 */
@Service
public class ChildProjectAdminCreateLogic implements ILogics<Map<String, Object>> {

    private Logger logger = LoggerFactory.getLogger("ChildProjectAdminCreateLogic");

    public ResultEnum exec(Map<String, Object> map) throws LogicsException {
        try {
            TblChildProjectInfo childProjectInfo = (TblChildProjectInfo) map.get("childProjectInfo");
            TblProjectBasicInfo projectBasicInfo = (TblProjectBasicInfo) map.get("projectBasicInfo");

            String projectPath = projectBasicInfo.getProjectPath().trim();
            if (!projectPath.endsWith("//")) {
                projectPath += "//";
                projectBasicInfo.setProjectPath(projectPath);
            }

            projectPath += projectBasicInfo.getProjectEng() + "//";
            projectPath += projectBasicInfo.getProjectEng() + "-" + childProjectInfo.getChildProjectEng();
            childProjectInfo.setProjectPath(projectPath);
            String pachagesPath = projectBasicInfo.getPackages().replace(".", "//");

            File file = new File(projectPath + "//src//main//resources//spring.xml");
            if (!file.exists()) {
                file.createNewFile();
            }
            StringBuffer sb = new StringBuffer();
            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> \n");
            sb.append("<beans xmlns=\"http://www.springframework.org/schema/beans\" \n");
            sb.append("			xmlns:util=\"http://www.springframework.org/schema/util\" \n");
            sb.append("			xmlns:aop=\"http://www.springframework.org/schema/aop\" \n");
            sb.append("			xmlns:context=\"http://www.springframework.org/schema/context\" \n");
            sb.append("			xmlns:tx=\"http://www.springframework.org/schema/tx\" \n");
            sb.append("			xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n");
            sb.append("			xmlns:mongo=\"http://www.springframework.org/schema/data/mongo\" \n");
            sb.append("			xmlns:task=\"http://www.springframework.org/schema/task\" \n");
            sb.append("			xsi:schemaLocation=\"http://www.springframework.org/schema/aop \n");
            sb.append("								http://www.springframework.org/schema/aop/spring-aop.xsd \n");
            sb.append("								http://www.springframework.org/schema/beans \n");
            sb.append("								http://www.springframework.org/schema/beans/spring-beans.xsd\n");
            sb.append("								http://www.springframework.org/schema/context \n");
            sb.append("								http://www.springframework.org/schema/context/spring-context.xsd \n");
            sb.append("								http://www.springframework.org/schema/tx \n");
            sb.append("								http://www.springframework.org/schema/tx/spring-tx.xsd \n");
            sb.append("								http://www.springframework.org/schema/task \n");
            sb.append("								http://www.springframework.org/schema/task/spring-task-3.1.xsd \n");
            sb.append("								http://www.springframework.org/schema/util \n");
            sb.append("								http://www.springframework.org/schema/util/spring-util-3.1.xsd\">\n");
            sb.append("  \n");
            sb.append("  <aop:aspectj-autoproxy proxy-target-class=\"true\" /> \n");
            sb.append("  <context:annotation-config /> \n");
            if ("com.gomeplus".equals(projectBasicInfo.getPackages())) {
                sb.append("  <context:component-scan base-package=\"com.gomeplus\" /> \n");
            } else {
                sb.append("  <context:component-scan base-package=\"" + projectBasicInfo.getPackages() + ",com.gomeplus\" /> \n");
            }
            sb.append("  \n");
            sb.append("  <import resource=\"classpath*:/frame/ftl-frame-servlet.xml\" /> \n");
            sb.append("  <import resource=\"classpath:/spring/spring-mvc-interceptor.xml\" /> \n");
            sb.append("  \n");
            sb.append("  <import resource=\"classpath*:/spring/" + projectBasicInfo.getProjectEng() + "-jdbc-props.xml\" /> \n");
            sb.append("  <import resource=\"classpath*:/spring/" + projectBasicInfo.getProjectEng() + "-dataSource.xml\" /> \n");
            sb.append("  \n");
            sb.append("  <bean id=\"springBeanFactory\" class=\"" + projectBasicInfo.getPackages() + ".frame.factory.SpringBeanFactory\" /> \n");
            sb.append("  \n");
            sb.append("  <import resource=\"classpath*:/dubbo/dubbo-head.xml\" /> \n");
            sb.append("  <import resource=\"classpath*:/" + projectBasicInfo.getProjectEng() + "/" + projectBasicInfo.getProjectEng() + "-dubbo-consumer.xml\" /> \n");
            sb.append("  <import resource=\"spring/redis-dubbo-consumer.xml\" /> \n");
            sb.append("</beans>\n");
            OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(projectPath + "//src//main//resources//spring.xml"), "UTF-8");
            BufferedWriter writer = new BufferedWriter(write);
            writer.write(sb.toString());
            writer.flush();
            writer.close();

            file = new File(projectPath + "//src//main//resources//log4j.properties");
            if (!file.exists()) {
                file.createNewFile();
            }
            sb = new StringBuffer();
            sb.append("log4j.rootLogger=DEBUG,stdout\n");
            sb.append(" \n");
            sb.append("log4j.appender.stdout=org.apache.log4j.ConsoleAppender\n");
            sb.append("log4j.appender.stdout.layout=org.apache.log4j.PatternLayout\n");
            sb.append("log4j.appender.stdout.DatePattern='_'yyyy-MM-dd'.log'\n");
            sb.append("log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH\\:mm\\:ss} %p [%c] %m%n\n");
            sb.append("      \n");
            sb.append("log4j.appender.fileAppender=org.apache.log4j.DailyRollingFileAppender\n");
            sb.append("log4j.appender.fileAppender.File=../logs/" + projectBasicInfo.getProjectEng() + "_admin_info.log\n");
            sb.append("log4j.appender.fileAppender.DatePattern='_'yyyy-MM-dd'.log'\n");
            sb.append("log4j.appender.fileAppender.Threshold=INFO\n");
            sb.append("log4j.appender.fileAppender.Encoding=UTF-8\n");
            sb.append("log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout\n");
            sb.append("log4j.appender.fileAppender.layout.ConversionPattern=%-d{yyyyMMdd HH:mm:ss SSS}-->[%-5p]--[%c{1}]--%m%\n");
            sb.append("\n");
            sb.append("log4j.appender.ibatis=org.apache.log4j.DailyRollingFileAppender\n");
            sb.append("log4j.appender.ibatis.File=../logs/" + projectBasicInfo.getProjectEng() + "_service_ibatis.log\n");
            sb.append("log4j.appender.ibatis.DatePattern='_'yyyy-MM-dd'.log'\n");
            sb.append("log4j.appener.ibatis.Threshold=DEBUG\n");
            sb.append("log4j.appender.ibatis.Encoding=UTF-8\n");
            sb.append("log4j.appender.ibatis.layout=org.apache.log4j.PatternLayout\n");
            sb.append("log4j.appender.ibatis.layout.ConversionPattern=%-d{yyyyMMdd HH:mm:ss SSS}-->[%-5p]--[%c{1}]--%m%nd\n");
            write = new OutputStreamWriter(new FileOutputStream(projectPath + "//src//main//resources//log4j.properties"), "UTF-8");
            writer = new BufferedWriter(write);
            writer.write(sb.toString());
            writer.flush();
            writer.close();

            file = new File(projectPath + "//src//main//resources//spring");
            if (!file.exists()) {
                file.mkdirs();
            }

            file = new File(projectPath + "//src//main//resources//spring//redis-dubbo-consumer.xml");
            if (!file.exists()) {
                file.createNewFile();
            }
            sb = new StringBuffer();
            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            sb.append("<beans xmlns=\"http://www.springframework.org/schema/beans\"\n");
            sb.append("	xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dubbo=\"http://code.alibabatech.com/schema/dubbo\"\n");
            sb.append("	xsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd\n");
            sb.append("	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd\">\n");

            sb.append("	<bean id=\"jjcsGcache\" class=\"redis.GcacheClient\" destroy-method=\"close\">\n");
            sb.append("	<!-- zookeeperAddress -->\n");
            sb.append("	<constructor-arg value=\"${dubbo.run.redis}\" />\n");
            sb.append("	<!-- businessName -->\n");
            sb.append("	<constructor-arg value=\"${redis.businessName}\" />\n");
            sb.append("	</bean>\n");

            sb.append("</beans>\n");
            write = new OutputStreamWriter(new FileOutputStream(projectPath + "//src//main//resources//spring//redis-dubbo-consumer.xml"), "UTF-8");
            writer = new BufferedWriter(write);
            writer.write(sb.toString());
            writer.flush();
            writer.close();

            file = new File(projectPath + "//src//main//resources//props//dev");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//resources//props//dev//" + projectBasicInfo.getProjectEng() + "-admin.properties");
            if (!file.exists()) {
                file.createNewFile();
            }
            file = new File(projectPath + "//src//main//resources//props//uat");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//resources//props//uat//" + projectBasicInfo.getProjectEng() + "-admin.properties");
            if (!file.exists()) {
                file.createNewFile();
            }
            file = new File(projectPath + "//src//main//resources//props//live");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//resources//props//live//" + projectBasicInfo.getProjectEng() + "-admin.properties");
            if (!file.exists()) {
                file.createNewFile();
            }
            file = new File(projectPath + "//src//main//resources//props//pre");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//resources//props//pre//" + projectBasicInfo.getProjectEng() + "-admin.properties");
            if (!file.exists()) {
                file.createNewFile();
            }

            file = new File(projectPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "-admin.properties");
            if (!file.exists()) {
                file.createNewFile();
            }
            file = new File(projectPath + "//src//main//webapp//js");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//webapp//css");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//webapp//images");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//webapp//WEB-INF//page");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//webapp//WEB-INF//web.xml");
            if (!file.exists()) {
                file.createNewFile();
            }
            sb = new StringBuffer();
            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
            sb.append("<web-app xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n");
            sb.append("	xmlns=\"http://java.sun.com/xml/ns/javaee\" \n");
            sb.append("	xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd\" \n");
            sb.append("	version=\"2.5\" metadata-complete=\"true\">\n");
            sb.append("  <context-param>\n");
            sb.append("  	<param-name>propertiesConfigLocation</param-name> \n");
            sb.append("  	<param-value>/props/" + projectBasicInfo.getProjectEng() + "-admin.properties</param-value>\n");
            sb.append("  </context-param>\n");
            sb.append("  <listener>\n");
            sb.append("  	<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> \n");
            sb.append("  </listener>\n");
            sb.append("  <listener>\n");
            sb.append("  	<listener-class>com.gomeplus.frame.listener.PropertiesConfigListener</listener-class> \n");
            sb.append("  </listener>\n");
            sb.append("  <servlet>\n");
            sb.append("  	<servlet-name>springmvc</servlet-name> \n");
            sb.append("  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>\n");
            sb.append("  	<init-param>\n");
            sb.append("  		<param-name>publishContext</param-name> \n");
            sb.append("  		<param-value>false</param-value> \n");
            sb.append("  	</init-param>\n");
            sb.append("  	<init-param>\n");
            sb.append("  		<param-name>contextConfigLocation</param-name> \n");
            sb.append("  		<param-value>classpath:spring.xml</param-value> \n");
            sb.append("  	</init-param>\n");
            sb.append("  	<load-on-startup>1</load-on-startup> \n");
            sb.append("  </servlet>\n");
            sb.append("  <servlet-mapping>\n");
            sb.append("  	<servlet-name>springmvc</servlet-name> \n");
            sb.append("  	<url-pattern>*.dhtml</url-pattern> \n");
            sb.append(" 	 <url-pattern>*.json</url-pattern> \n");
            sb.append("  </servlet-mapping>\n");
            sb.append("  <filter>\n");
            sb.append("  	<filter-name>HiddenHttpMethodFilter</filter-name> \n");
            sb.append("  	<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> \n");
            sb.append("  </filter>\n");
            sb.append("  <filter-mapping>\n");
            sb.append("  	<filter-name>HiddenHttpMethodFilter</filter-name> \n");
            sb.append("  	<servlet-name>springmvc</servlet-name> \n");
            sb.append("  </filter-mapping>\n");
            sb.append("  <filter>\n");
            sb.append("  	<filter-name>EncodingFilter</filter-name> \n");
            sb.append("  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> \n");
            sb.append("  	<init-param>\n");
            sb.append("  		<param-name>encoding</param-name> \n");
            sb.append("  		<param-value>UTF-8</param-value> \n");
            sb.append(" 	</init-param>\n");
            sb.append("  </filter>\n");
            sb.append("  <filter-mapping>\n");
            sb.append("  	<filter-name>EncodingFilter</filter-name> \n");
            sb.append("  	<url-pattern>/*</url-pattern> \n");
            sb.append("  </filter-mapping>\n");
            sb.append("  <error-page>\n");
            sb.append("  	<error-code>404</error-code> \n");
            sb.append("  	<location>/resultController/404error.dhtml</location> \n");
            sb.append("  </error-page>\n");
            sb.append("  <error-page>\n");
            sb.append("  	<error-code>500</error-code> \n");
            sb.append("  	<location>/resultController/500error.dhtml</location> \n");
            sb.append("  </error-page>\n");
            sb.append("</web-app>\n");
            write = new OutputStreamWriter(new FileOutputStream(projectPath + "//src//main//webapp//WEB-INF//web.xml"), "UTF-8");
            writer = new BufferedWriter(write);
            writer.write(sb.toString());
            writer.flush();
            writer.close();

            file = new File(projectPath + "//src//main//resources//spring//spring-mvc-interceptor.xml");
            if (!file.exists()) {
                file.createNewFile();
            }
            sb = new StringBuffer();
            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
            sb.append(" <beans xmlns=\"http://www.springframework.org/schema/beans\"\n");
            sb.append("	 xmlns:util=\"http://www.springframework.org/schema/util\" \n");
            sb.append("	 xmlns:aop=\"http://www.springframework.org/schema/aop\" \n");
            sb.append("	 xmlns:context=\"http://www.springframework.org/schema/context\" \n");
            sb.append("	 xmlns:tx=\"http://www.springframework.org/schema/tx\" \n");
            sb.append("	 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n");
            sb.append("	 xmlns:mongo=\"http://www.springframework.org/schema/data/mongo\" \n");
            sb.append("	 xmlns:task=\"http://www.springframework.org/schema/task\" \n");
            sb.append("	 xmlns:mvc=\"http://www.springframework.org/schema/mvc\" \n");
            sb.append("	 xsi:schemaLocation=\"http://www.springframework.org/schema/aop \n");
            sb.append("		 http://www.springframework.org/schema/aop/spring-aop.xsd \n");
            sb.append("		 http://www.springframework.org/schema/beans \n");
            sb.append("		 http://www.springframework.org/schema/beans/spring-beans.xsd \n");
            sb.append("		 http://www.springframework.org/schema/context \n");
            sb.append("		 http://www.springframework.org/schema/context/spring-context.xsd \n");
            sb.append("		 http://www.springframework.org/schema/tx \n");
            sb.append("		 http://www.springframework.org/schema/tx/spring-tx.xsd \n");
            sb.append("		 http://www.springframework.org/schema/task \n");
            sb.append("		 http://www.springframework.org/schema/task/spring-task-3.1.xsd \n");
            sb.append("		 http://www.springframework.org/schema/util \n");
            sb.append("		 http://www.springframework.org/schema/util/spring-util-3.1.xsd \n");
            sb.append("		 http://www.springframework.org/schema/mvc \n");
            sb.append("		 http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd\">\n");
            sb.append("  <mvc:interceptors>\n");
            sb.append("  	<mvc:interceptor>\n");
            sb.append("  		<mvc:mapping path=\"/**/*\" /> \n");
            sb.append("  		<bean class=\"" + projectBasicInfo.getPackages() + "." + projectBasicInfo.getProjectEng() + ".admin.interceptor.GomeUserLoginInterceptor\">\n");
            sb.append("  			<property name=\"allowUrls\">\n");
            sb.append("  				<list>\n");
            sb.append("  					<value>payController/404error.dhtml</value> \n");
            sb.append("  					<value>payController/500error.dhtml</value> \n");
            sb.append("  					<value>tests/tests.dhtml</value> \n");
            sb.append("  				</list>\n");
            sb.append("  			</property>\n");
            sb.append("  		</bean>\n");
            sb.append("  	</mvc:interceptor>\n");
            sb.append("  </mvc:interceptors>\n");
            sb.append("</beans>\n");
            write = new OutputStreamWriter(new FileOutputStream(projectPath + "//src//main//resources//spring//spring-mvc-interceptor.xml"), "UTF-8");
            writer = new BufferedWriter(write);
            writer.write(sb.toString());
            writer.flush();
            writer.close();

            file = new File(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//admin//controller");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//admin//interceptor");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//admin//util");
            if (!file.exists()) {
                file.mkdirs();
            }

            file = new File(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//admin//interceptor//GomeUserLoginInterceptor.java");
            if (!file.exists()) {
                file.createNewFile();
            }
            sb = new StringBuffer();
            sb.append("package " + projectBasicInfo.getPackages() + "." + projectBasicInfo.getProjectEng() + ".admin.interceptor;\n");
            sb.append("\n");
            sb.append("import java.io.IOException;\n");
            sb.append("import javax.servlet.http.HttpServletRequest;\n");
            sb.append("import javax.servlet.http.HttpServletResponse;\n");
            sb.append("import org.slf4j.Logger;\n");
            sb.append("import org.slf4j.LoggerFactory;\n");
            sb.append("import org.springframework.web.servlet.HandlerInterceptor;\n");
            sb.append("import org.springframework.web.servlet.ModelAndView;\n");
            sb.append("import com.gomeplus.frame.cache.GlobalApplicationCache;\n");
            sb.append("\n");
            sb.append("public class GomeUserLoginInterceptor implements HandlerInterceptor{\n");
            sb.append("\n");
            sb.append("	private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());\n");
            sb.append("	\n");
            sb.append("	private String[] allowUrls = null;\n");
            sb.append("	\n");
            sb.append("	public String[] getAllowUrls() {\n");
            sb.append("		return allowUrls;\n");
            sb.append("	}\n");
            sb.append("\n");
            sb.append("	public void setAllowUrls(String[] allowUrls) {\n");
            sb.append("		this.allowUrls = allowUrls;\n");
            sb.append("	}\n");
            sb.append("	\n");
            sb.append("	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {\n");
            sb.append("		String basePath = request.getScheme()+\"://\" + request.getServerName() + \":\" + request.getServerPort() + request.getContextPath()+\"/\";\n");
            sb.append("		request.setAttribute(\"basePath\", basePath);\n");
            sb.append("		return true;\n");
            sb.append("	}\n");
            sb.append("\n");
            sb.append("	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,\n");
            sb.append("			ModelAndView modelAndView) throws Exception {}\n");
            sb.append("\n");
            sb.append("	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)\n");
            sb.append("			throws Exception {}\n");
            sb.append("\n");
            sb.append("}\n");
            write = new OutputStreamWriter(new FileOutputStream(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//admin//interceptor//GomeUserLoginInterceptor.java"), "UTF-8");
            writer = new BufferedWriter(write);
            writer.write(sb.toString());
            writer.flush();
            writer.close();

            file = new File(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//admin//controller//UserLoginController.java");
            if (!file.exists()) {
                file.createNewFile();
            }
            sb = new StringBuffer();
            sb.append("package " + projectBasicInfo.getPackages() + "." + projectBasicInfo.getProjectEng() + ".admin.controller;\n");
            sb.append("\n");
            sb.append("import javax.servlet.http.HttpServletRequest;\n");
            sb.append("import javax.servlet.http.HttpServletResponse;\n");
            sb.append("\n");
            sb.append("import org.springframework.stereotype.Controller;\n");
            sb.append("import org.springframework.ui.Model;\n");
            sb.append("import org.springframework.web.bind.annotation.RequestMapping;\n");
            sb.append("import com.gomeplus.frame.controller.AbstractAdminController;\n");
            sb.append("\n");
            sb.append("@Controller\n");
            sb.append("@RequestMapping(\"/login\")\n");
            sb.append("public class UserLoginController extends AbstractAdminController{\n");
            sb.append("\n");
            sb.append("	@RequestMapping(\"/login\")\n");
            sb.append("	public String login(Model model,HttpServletRequest request, HttpServletResponse response) {\n");
            sb.append("		\n");
            sb.append("		return \"login\";\n");
            sb.append("	}\n");
            sb.append("\n");
            sb.append("	@RequestMapping(\"/loginCheck\")\n");
            sb.append("	public String loginCheck(Model model,HttpServletRequest request, HttpServletResponse response) {\n");
            sb.append("		\n");
            sb.append("		return \"default\";\n");
            sb.append("	}\n");
            sb.append("\n");
            sb.append("}\n");
            write = new OutputStreamWriter(new FileOutputStream(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//admin//controller//UserLoginController.java"), "UTF-8");
            writer = new BufferedWriter(write);
            writer.write(sb.toString());
            writer.flush();
            writer.close();

            file = new File(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//admin//controller//AdminDictionaryController.java");
            if (!file.exists()) {
                file.createNewFile();
            }
            sb = new StringBuffer();
            sb.append("/**\n");
            sb.append("* @Title: AdminDataDictionaryController.java\n");
            sb.append("* @Package " + projectBasicInfo.getPackages() + "." + projectBasicInfo.getProjectEng() + ".admin.controller \n");
            sb.append("* @Description: 数据字典获取控制器\n");
            sb.append("* @author chenmin-ds   \n");
            sb.append("* @date 2015年3月17日 上午10:30:31 \n");
            sb.append("* @company cn.com.gome\n");
            sb.append("* @version V1.0   \n");
            sb.append("*/ \n");
            sb.append("\n");
            sb.append("\n");
            sb.append("package " + projectBasicInfo.getPackages() + "." + projectBasicInfo.getProjectEng() + ".admin.controller;\n");
            sb.append("\n");
            sb.append("import java.util.ArrayList;\n");
            sb.append("import java.util.Collections;\n");
            sb.append("import java.util.List;\n");
            sb.append("\n");
            sb.append("import javax.servlet.http.HttpServletRequest;\n");
            sb.append("import javax.servlet.http.HttpServletResponse;\n");
            sb.append("\n");
            sb.append("import net.sf.json.JSONArray;\n");
            sb.append("\n");
            sb.append("import org.springframework.stereotype.Controller;\n");
            sb.append("import org.springframework.web.bind.annotation.ModelAttribute;\n");
            sb.append("import org.springframework.web.bind.annotation.RequestMapping;\n");
            sb.append("\n");
            sb.append("import com.gomeplus.frame.cache.DictionaryVo;\n");
            sb.append("import com.gomeplus.frame.cache.GlobalDataDictionaryCache;\n");
            sb.append("import com.gomeplus.frame.controller.AbstractAdminController;\n");
            sb.append("\n");
            sb.append("/**\n");
            sb.append(" * @ClassName: AdminDataDictionaryController\n");
            sb.append(" * @Description: 数据字典获取控制器\n");
            sb.append(" * @author chenmin-ds\n");
            sb.append(" * @date 2015年3月4日 下午4:41:58\n");
            sb.append(" */\n");
            sb.append("@Controller\n");
            sb.append("@RequestMapping(\"/dictionary\")\n");
            sb.append("public class AdminDictionaryController extends AbstractAdminController{\n");
            sb.append("\n");
            sb.append("	@RequestMapping(\"/select\")\n");
            sb.append("	public void selects(HttpServletRequest request, HttpServletResponse response) {\n");
            sb.append("		List<DictionaryVo> list = new ArrayList<DictionaryVo>();\n");
            sb.append("		try {\n");
            sb.append("			String key = request.getParameter(\"key\");\n");
            sb.append("			List<DictionaryVo> obj = GlobalDataDictionaryCache.getInstance().getIdValue(key);\n");
            sb.append("			if (obj != null) {\n");
            sb.append("				Collections.addAll(list,  new  DictionaryVo[obj.size()]);   \n");
            sb.append("				Collections.copy(list, obj);   \n");
            sb.append("				DictionaryVo idvalue = new DictionaryVo(null,\"全部\",true);\n");
            sb.append("				list.add(0, idvalue);\n");
            sb.append("			}\n");
            sb.append("			ajaxJsonMessage(response,JSONArray.fromObject(list).toString());\n");
            sb.append("		} catch (Exception e) {\n");
            sb.append("			e.printStackTrace();\n");
            sb.append("		}\n");
            sb.append("	}\n");
            sb.append("	\n");
            sb.append("	@RequestMapping(\"/selectv\")\n");
            sb.append("	public void selectsv(HttpServletRequest request, HttpServletResponse response) {\n");
            sb.append("		try {\n");
            sb.append("			String key = request.getParameter(\"key\");\n");
            sb.append("			List<DictionaryVo> obj = GlobalDataDictionaryCache.getInstance().getIdValue(key);\n");
            sb.append("			ajaxJsonMessage(response,JSONArray.fromObject(obj).toString());\n");
            sb.append("		} catch (Exception e) {\n");
            sb.append("			e.printStackTrace();\n");
            sb.append("		}\n");
            sb.append("	}\n");
            sb.append("	\n");
            sb.append("	@RequestMapping(\"/selectl\")\n");
            sb.append("	public void selectl(HttpServletRequest request, HttpServletResponse response) {\n");
            sb.append("		List<DictionaryVo> list = new ArrayList<DictionaryVo>();\n");
            sb.append("		try {\n");
            sb.append("			String key = request.getParameter(\"key\");\n");
            sb.append("			List<DictionaryVo> obj = GlobalDataDictionaryCache.getInstance().getIdValue(key);\n");
            sb.append("			if (obj != null) {\n");
            sb.append("				Collections.addAll(list,  new  DictionaryVo[obj.size()]);   \n");
            sb.append("				Collections.copy(list, obj);   \n");
            sb.append("				DictionaryVo idvalue = new DictionaryVo(null,\"无\",true);\n");
            sb.append("				list.add(0, idvalue);\n");
            sb.append("			}\n");
            sb.append("			ajaxJsonMessage(response,JSONArray.fromObject(list).toString());\n");
            sb.append("		} catch (Exception e) {\n");
            sb.append("			e.printStackTrace();\n");
            sb.append("		}\n");
            sb.append("	}\n");
            sb.append("}\n");
            write = new OutputStreamWriter(new FileOutputStream(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//admin//controller//AdminDictionaryController.java"), "UTF-8");
            writer = new BufferedWriter(write);
            writer.write(sb.toString());
            writer.flush();
            writer.close();

            file = new File(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//admin//logic//impl");
            if (!file.exists()) {
                file.mkdirs();
            }

            file = new File(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//admin//logic//impl//CacheServiceImpl.java");
            if (!file.exists()) {
                file.createNewFile();
            }
            sb = new StringBuffer();
            sb.append("package " + projectBasicInfo.getPackages() + "." + projectBasicInfo.getProjectEng() + ".admin.logic.impl;\n");
            sb.append("\n");
            sb.append("import java.util.ArrayList;\n");
            sb.append("import java.util.List;\n");
            sb.append("\n");
            sb.append("import javax.annotation.PostConstruct;\n");
            sb.append("\n");
            sb.append("import org.slf4j.Logger;\n");
            sb.append("import org.slf4j.LoggerFactory;\n");
            sb.append("import org.springframework.stereotype.Service;\n");
            sb.append("\n");
            sb.append("import com.gomeplus.frame.cache.DictionaryVo;\n");
            sb.append("import com.gomeplus.frame.cache.GlobalDataDictionaryCache;\n");
            sb.append("\n");
            sb.append("import " + projectBasicInfo.getPackages() + "." + projectBasicInfo.getProjectEng() + ".admin.logic.CacheService;\n");
            sb.append("\n");
            sb.append("/**\n");
            sb.append(" * @ClassName: CacheServiceImpl\n");
            sb.append(" * @Description: 缓存参数初始化接口实现\n");
            sb.append(" * @author chenmin-ds\n");
            sb.append(" * @date 2015年5月13日 下午4:47:02\n");
            sb.append(" */\n");
            sb.append("@Service\n");
            sb.append("public class CacheServiceImpl implements CacheService {\n");
            sb.append("\n");
            sb.append("	private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());\n");
            sb.append("	\n");
            sb.append("	@PostConstruct\n");
            sb.append("	private void inits() {\n");
            sb.append("		logger.info(\"系统启动时初始化静态参数开始...\");\n");
            sb.append("		setSysDic();\n");
            sb.append("		logger.info(\"系统启动时初始化静态参数成功\");\n");
            sb.append("	}\n");
            sb.append("	\n");
            sb.append("	public void cacheUpdate() {\n");
            sb.append("		logger.info(\"系统更新初始化静态参数开始...\");\n");
            sb.append("		setSysDic();\n");
            sb.append("		logger.info(\"系统更新初始化静态参数成功\");\n");
            sb.append("	}\n");
            sb.append("\n");
            sb.append("	private void setSysDic(){\n");
            sb.append("		List<DictionaryVo> list = new ArrayList<DictionaryVo>();\n");
            sb.append("		list.add(new DictionaryVo(\"010\",\"测试1\"));\n");
            sb.append("		list.add(new DictionaryVo(\"020\",\"测试2\"));\n");
            sb.append("		list.add(new DictionaryVo(\"030\",\"测试3\"));\n");
            sb.append("		list.add(new DictionaryVo(\"040\",\"测试4\"));\n");
            sb.append("		GlobalDataDictionaryCache.getInstance().putIdValue(\"TEST_TEST\", list);\n");
            sb.append("	}\n");
            sb.append("}\n");

            write = new OutputStreamWriter(new FileOutputStream(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//admin//logic//impl//CacheServiceImpl.java"), "UTF-8");
            writer = new BufferedWriter(write);
            writer.write(sb.toString());
            writer.flush();
            writer.close();

            file = new File(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//admin//logic//CacheService.java");
            if (!file.exists()) {
                file.createNewFile();
            }
            sb = new StringBuffer();
            sb.append("package " + projectBasicInfo.getPackages() + "." + projectBasicInfo.getProjectEng() + ".admin.logic;\n");
            sb.append("\n");
            sb.append("/**\n");
            sb.append(" * @ClassName: CacheServiceImpl\n");
            sb.append(" * @Description: 缓存参数初始化接口实现\n");
            sb.append(" * @author chenmin-ds\n");
            sb.append(" * @date 2015年5月13日 下午4:47:02\n");
            sb.append(" */\n");
            sb.append("public interface CacheService {\n");
            sb.append("\n");
            sb.append("	public void cacheUpdate();\n");
            sb.append("}\n");
            write = new OutputStreamWriter(new FileOutputStream(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//admin//logic//CacheService.java"), "UTF-8");
            writer = new BufferedWriter(write);
            writer.write(sb.toString());
            writer.flush();
            writer.close();

            ResourceBundle rb = ResourceBundle.getBundle("generator");
            String _projectPath = rb.getString("projectPath");

            String filePath = _projectPath + "//src//main//webapp//template//css//";
            String copyPath = projectPath + "//src//main//webapp//";
            copyFolderWithSelf(filePath, copyPath);

            filePath = _projectPath + "//src//main//webapp//template//js//";
            copyPath = projectPath + "//src//main//webapp//";
            copyFolderWithSelf(filePath, copyPath);

            filePath = _projectPath + "//src//main//webapp//template//images//";
            copyPath = projectPath + "//src//main//webapp//";
            copyFolderWithSelf(filePath, copyPath);

            filePath = _projectPath + "//src//main//webapp//template//page//login.ftl";
            copyPath = projectPath + "//src//main//webapp//WEB-INF//page//login.ftl";
            CopySingleFile(filePath, copyPath);

            filePath = _projectPath + "//src//main//webapp//template//page//default.ftl";
            copyPath = projectPath + "//src//main//webapp//WEB-INF//page//default.ftl";
            CopySingleFile(filePath, copyPath);

            filePath = _projectPath + "//src//main//webapp//template//page//success.ftl";
            copyPath = projectPath + "//src//main//webapp//WEB-INF//page//success.ftl";
            CopySingleFile(filePath, copyPath);


            return ResultEnum.OK;
        } catch (Exception e) {
            logger.error("创建子项目异常：", e);
        }
        return ResultEnum.PART_CASE_01;
    }

    /**
     * 复制整个文件夹的内容(含自身)
     *
     * @param oldPath 准备拷贝的目录
     * @param newPath 指定绝对路径的新目录
     * @return
     */
    private void copyFolderWithSelf(String oldPath, String newPath) {
        try {
            new File(newPath).mkdirs(); //如果文件夹不存在 则建立新文件夹   
            File dir = new File(oldPath);
            // 目标   
            newPath += File.separator + dir.getName();
            File moveDir = new File(newPath);
            if (dir.isDirectory()) {
                if (!moveDir.exists()) {
                    moveDir.mkdirs();
                }
            }
            String[] file = dir.list();
            File temp = null;
            for (int i = 0; i < file.length; i++) {
                if (oldPath.endsWith(File.separator)) {
                    temp = new File(oldPath + file[i]);
                } else {
                    temp = new File(oldPath + File.separator + file[i]);
                }
                if (temp.isFile()) {
                    FileInputStream input = new FileInputStream(temp);
                    FileOutputStream output = new FileOutputStream(newPath +
                            "/" +
                            (temp.getName()).toString());
                    byte[] b = new byte[1024 * 5];
                    int len;
                    while ((len = input.read(b)) != -1) {
                        output.write(b, 0, len);
                    }
                    output.flush();
                    output.close();
                    input.close();
                }
                if (temp.isDirectory()) { //如果是子文件夹   
                    copyFolderWithSelf(oldPath + "/" + file[i], newPath);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 复制单个文件(可更名复制)
     *
     * @param oldPathFile 准备复制的文件源
     * @param newPathFile 拷贝到新绝对路径带文件名(注：目录路径需带文件名)
     * @return
     */
    @SuppressWarnings({"unused", "resource"})
    private void CopySingleFile(String oldPathFile, String newPathFile) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPathFile);
            if (oldfile.exists()) { //文件存在时   
                InputStream inStream = new FileInputStream(oldPathFile); //读入原文件   
                FileOutputStream fs = new FileOutputStream(newPathFile);
                byte[] buffer = new byte[1444];
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; //字节数 文件大小   
                    //System.out.println(bytesum);   
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
